package com.company.cloud.club.modules.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.cloud.club.modules.core.util.ValidateUtil;
import com.company.cloud.club.modules.db.dao.ClubActivityMapper;
import com.company.cloud.club.modules.db.domain.ClubActivity;
import com.company.cloud.club.modules.db.domain.ClubActivityRelative;
import com.company.cloud.club.modules.service.ClubActivityRelativeService;
import com.company.cloud.club.modules.service.ClubActivityService;
import com.company.cloud.club.modules.utils.LiveUtils;
import com.company.cloud.club.modules.vo.LiveInfoVo;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * *****************************************************************************
 * <p>
 * 功能名           ：com.qxzychina..service.impl
 * 系统名           ：
 * <p>
 * *****************************************************************************
 * Modification History
 * <p>
 * Date        Name                    Reason for Change
 * ----------  ----------------------  -----------------------------------------
 * 2020/7/18     zhangxu                 Created
 */
@Service
public class ClubActivityServiceImpl extends ServiceImpl<ClubActivityMapper, ClubActivity> implements ClubActivityService {

    @Autowired
    private ClubActivityRelativeService clubActivityRelativeService;

    @Override
    public List<ClubActivity> queryHot(Long curr) {
        return baseMapper.queryHot(curr);
    }

    @Override
    public List<ClubActivity> queryAll() {
        return baseMapper.queryAll();
    }

    @Override
    public List<ClubActivity> queryHotHistory(Long curr) {
        return baseMapper.queryHot(curr);
    }

    @Override
    public boolean removeActivity(String id) {
        return baseMapper.removeActivity(id);
    }

    @Override
    public List<ClubActivity> selectByCondition(String condition) {
        return baseMapper.selectByCondition(condition);
    }

    @Override
    public ClubActivity getLastClubActivity(String id) {
        ClubActivityRelative relative = clubActivityRelativeService.selectLastActivity(id);
        if (ValidateUtil.isValid(relative)) {
            String activityId = relative.getActivityId();
            ClubActivity activity = baseMapper.selectById(activityId);
            if (ValidateUtil.isValid(activity) && System.currentTimeMillis() < activity.getEndTime()) {
                return activity;
            }
        }
        return null;
    }

    @Override
    public String getLiveStream(String streamName) {
        return LiveUtils.generatePushUrl("live", "artplus-" + streamName, 7200);
    }

    @Override
    public String getPlayerStream(String streamName) {
        return LiveUtils.generatePullHlsUrl("live", "artplus-" + streamName, 7200);
    }

    @Override
    public String getPlayerStreamRtmp(String streamName) {
        return LiveUtils.generatePullRtmpUrl("live", "artplus-" + streamName, 7200);
    }

    @Override
    public List<ClubActivity> queryByClubId(String clubId) {
        List<ClubActivity> list = new ArrayList<>();
        List<ClubActivityRelative> relativeList = clubActivityRelativeService.selectByClubId(clubId);
        for (ClubActivityRelative relative : relativeList) {
            String activityId = relative.getActivityId();
            ClubActivity activity = baseMapper.selectById(activityId);
            if (ValidateUtil.isValid(activity)) {
                if (activity.getDeleted() != 0) {
                    continue;
                }
                list.add(activity);
            }
        }
        return list;
    }

    @Override
    public void buildBackPlayUrl(LiveInfoVo vo, String streamName) {
        try {
            Credential cred = new Credential("AKIDCE0VkiQs36sOdbIjtmOkqfDTTBdoiMjp", "sTnrd9OM3UdV6RUGlzxe4D6i37vSihPr");
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("vod.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            VodClient client = new VodClient(cred, "", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            SearchMediaRequest req = new SearchMediaRequest();
            String[] sourceTypes1 = {"Record"};
            req.setSourceTypes(sourceTypes1);

            String[] streamIds1 = {"artplus-" + streamName};
            req.setStreamIds(streamIds1);

            SortBy sortBy1 = new SortBy();
            sortBy1.setField("CreateTime");
            sortBy1.setOrder("Asc");
            req.setSort(sortBy1);

            // 返回的resp是一个SearchMediaResponse的实例，与请求对象对应
            SearchMediaResponse resp = client.SearchMedia(req);
            // 输出json格式的字符串回包
            Long totalCount = resp.getTotalCount();
            if (totalCount > 0) {
                for (MediaInfo mediaInfo : resp.getMediaInfoSet()) {
                    MediaBasicInfo basicInfo = mediaInfo.getBasicInfo();
                    vo.addBackPlayUrl(basicInfo.getMediaUrl());
                    vo.addBackPlayRtmpUrl(basicInfo.getMediaUrl());
                }
            }
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
    }

}